/**
 * @author yinyuejia
 * @file 封装颜色常用方法
 * @include hex颜色转rgb颜色 HexToRgb;rgb颜色转hex颜色 RgbToHex;加深 darken;变淡 lighten;是不是较暗模式 isDark
 */

export default {
  // hex颜色转rgb颜色
  HexToRgb(str: any) {
    str = str.replace('#', '');
    const hxs = str.match(/../g);
    for (let i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16);
    return hxs;
  },
  // rgb颜色转hex颜色
  RgbToHex(a: any, b: any, c: any) {
    const hexs = [a.toString(16), b.toString(16), c.toString(16)];
    for (let i = 0; i < 3; i++) {
      if (hexs[i].length === 1) hexs[i] = `0${hexs[i]}`;
    }
    return `#${hexs.join('')}`;
  },
  // 加深
  darken(color: any, level: any) {
    const rgbc = this.HexToRgb(color);
    for (let i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level));
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  },
  // 变淡
  lighten(color: any, level: any) {
    const rgbc = this.HexToRgb(color);
    for (let i = 0; i < 3; i++) {
      rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i]);
    }
    return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  },
  // 是不是较暗模式
  isDark(color: any) {
    const rgbc = this.HexToRgb(color);
    return rgbc[0] * 0.299 + rgbc[1] * 0.587 + rgbc[2] * 0.114 < 192;
  },
};
